package xj.graph2d.tools;

import java.awt.Point;
import java.awt.geom.Point2D;
import java.util.Observer;

import javax.swing.AbstractButton;

import xj.graph2d.DrawCanvas;
import xj.graph2d.DrawShape;
import xj.graph2d.DrawShapeConstants;
import xj.graph2d.DrawToolAdapter;
import xj.graph2d.SimpleDrawDocument;
import xj.graph2d.SimpleDrawView;

public abstract class SimpleDrawTool 
  extends DrawToolAdapter 
  implements DrawShapeConstants {

  protected SimpleDrawTool() {
  }

  protected SimpleDrawTool(int cloneDrawAttrMode) {
    this.cloneDrawAttrMode = cloneDrawAttrMode;
  }

  public void start(Observer observer) {
    if (observer instanceof SimpleDrawView) {
      // System.out.println("SimpleDrawTool.start: okay");

      view = (SimpleDrawView) observer;
      doc = view.getSimpleDrawDocument();
      canvas = view.getCanvas();
    } else {
      // System.out.println("SimpleDrawTool.start: null");

      view = null;
      doc = null;
      canvas = null;
    }
  }

  public void end() {
    view = null;
    doc = null;
    canvas = null;
  }

  public boolean isGraphMode() {
    return graphMode;
  }

  public void setGraphMode(boolean graphMode) {
    this.graphMode = graphMode;
  }

  @Override
  public void mouseEntered(Observer observer, Point p, int modifiers) {
    start(observer);
  }

  @Override
  public void mouseDragged(Observer observer, Point p, int modifiers) {
    if (view != null) {
      displayLocation(p);
    }
  }

  @Override
  public void mouseMoved(Observer observer, Point p, int modifiers) {
    if (view != null) {
      displayLocation(p);
    }
  }

  public SimpleDrawTool getSwitchTool() {
    return switchTool;
  }

  public void setSwitchTool(SimpleDrawTool tool) {
    switchTool = tool;
  }

  public void setToolButton(AbstractButton button) {
    toolButton = button;
  }

  public void setActionShape(DrawShape shape) {
    //System.out.println("SimpleDrawTool.setActionShape() shape == null " + (shape == null));
    actionShape = shape;
  }

  public AbstractButton getToolButton() {
    return toolButton;
  }

  public SimpleDrawView getView() { 
    return view;
  }

  protected void switchTool() {
    if (switchTool != null) {
      view.setCurrentTool(switchTool);
      AbstractButton b = switchTool.getToolButton();
      if (b != null) {
	b.setSelected(true);
      }
    }
  }

  protected void displayLocation(Point p) {
    if (view != null) {
      Point2D cp = view.physicalToVirtual(p);
      if (cp != null) { 
	view.displayLocation((float) cp.getX(), (float) cp.getY());
      }
    }
  }

  protected boolean mouseButtonDown = false;

  protected Point2D prev;

  protected SimpleDrawView view;

  protected SimpleDrawDocument doc;

  protected DrawCanvas canvas;

  protected int cloneDrawAttrMode = NO_DRAW_ATTR;

  protected SimpleDrawTool switchTool = null;

  protected AbstractButton toolButton = null;

  protected boolean graphMode = false;

  protected DrawShape actionShape = null;

  protected static final boolean debug = false;

}
